//
//  WHContactsEditTableView.m
//  AddressBook
//
//  Created by  on 2020/7/28.
//  Copyright © 2020 wh. All rights reserved.
//

#import "WHContactsEditTableView.h"
#import "MacroDefinition.h"
#import "Masonry.h"
#import "WHContactsEditCell.h"
#import "WHSingleton.h"
#import "UIImageTool.h"
static NSString *editCellId = @"editCellId";

@interface WHContactsEditTableView() <UITableViewDelegate,UITableViewDataSource>

@property (strong,nonatomic) NSArray<NSString *> *contentArr;

@end

@implementation WHContactsEditTableView
{
    NSArray *_titleArray;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
        _titleArray = [NSArray arrayWithObjects:@"姓名",@"号码",@"地址", nil];
        self.tableFooterView = [[UIView alloc] init];
    }
    return self;
}

- (void)setContentWithArr:(NSArray<NSString *>*)contentArr
{
    self.contentArr = contentArr;
    for (int i = 0; i < contentArr.count; i++) {
        [WHSingleton setDataWithIndex:i content:self.contentArr[i]];
    }
}

#pragma mark - tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WHContactsEditCell *cell = [tableView dequeueReusableCellWithIdentifier:editCellId];
    if (!cell) {
        cell = [[WHContactsEditCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:editCellId];
    }
    if (self.contentArr.count != 0) {
        [cell setDetailCellDataWithTitle:_titleArray[indexPath.row] content:self.contentArr[indexPath.row]];
    }else{
        [cell setDetailCellDataWithTitle:_titleArray[indexPath.row] content:_titleArray[indexPath.row]];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return ScreenHeight*0.3+5;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    if (self.editTableViewDelegate && [self.editTableViewDelegate performSelector:@selector(editTableGetScrollContentOffsetY:)]) {
        [self.editTableViewDelegate editTableGetScrollContentOffsetY:offsetY];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (self.editTableViewDelegate && [self.editTableViewDelegate performSelector:@selector(editTableStopScrollAction)]) {
        [self.editTableViewDelegate editTableStopScrollAction];
    }
}


#pragma mark - --
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    if (touch) {
        [self endEditing:YES];
    }
}
#pragma mark - 懒加载

- (NSArray<NSString *> *)contentArr
{
    if (!_contentArr) {
        _contentArr = [[NSArray alloc] init];
    }
    return _contentArr;
}


@end
